package service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.VideoDao;
import entity.Video;
import service.VideoService;
import util.PageUtil;

@Service
public class VideoServiceImpl implements VideoService {

	@Autowired
	SqlSessionFactory factory;
	@Autowired
	PageUtil<Video> pageUtil;
	//查找收费视频
	
	@Override
	public List<Video> find() {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.find();
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}
	//根据用户账号查找他上传过的视频
	@Override
	public List<Video> findVideoByUserId(int userId) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findVideoByUserId(userId);
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}

	@Override
	public List<Video> findByState(int id) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findByState(id);
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}

	@Override
	public int updateVideoState(Video video) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			int result = dao.updateVideoState(video);
			return result;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}
	//根据上传时间和点击量查找视频
	public List<Video> findByNumberAndTime() {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findByNumberAndTime();
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return null;
	}
	//根据分类编号查找视频
	public List<Video> findByCategoryId(int categoryId) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findByCategoryId(categoryId);
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}
	//查找收费视频
	public List<Video> findByisCharge(int isCharge) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findByisCharge(isCharge);
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return null;
	}
	//根据关键字查找相关视频
	@Override
	public PageUtil<Video> findByName(String name,int pageIndex, int size) {
		SqlSession sqlSession=null;
		List<Video> list=null;
		int count = 0;
		try {
			sqlSession=factory.openSession();
			//操作数据，取得数据访问接口的代理对象
			 VideoDao dao = sqlSession.getMapper(VideoDao.class);
			 //num:跳过多少条数据
			 int num=(pageIndex-1)*size;
			 list=dao.findByName(name,num, size);
			 //查询后返回的数据数量
			 count=list.size() ;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		pageUtil.setPageIndex(pageIndex);
		pageUtil.setList(list);
		pageUtil.setCount(count);
		pageUtil.setPageNumber((int)Math.ceil((double)count/size));
		return pageUtil;
	}
	
	//按日期排序、分页
	@Override
	public PageUtil<Video> findByDateSort(int pageIndex, int size) {
		SqlSession sqlSession=null;
		List<Video> list=null;
		int count = 0;
		try {
			sqlSession=factory.openSession();
			//操作数据，取得数据访问接口的代理对象
			 VideoDao dao = sqlSession.getMapper(VideoDao.class);
			//数据库一共多少条数据
			 count= dao.findCount();
			 //num:跳过多少条数据
			 int num=(pageIndex-1)*size;
			 list=dao.findByDateSort(num, size);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		pageUtil.setPageIndex(pageIndex);
		pageUtil.setList(list);
		pageUtil.setCount(count);
		pageUtil.setPageNumber((int)Math.ceil((double)count/size));
		return pageUtil;
	}
	//按点击量排序
	@Override
	public PageUtil<Video> findByVideoClick(int pageIndex, int size) {
		SqlSession sqlSession=null;
		List<Video> list=null;
		int count = 0;
		try {
			sqlSession=factory.openSession();
			//操作数据，取得数据访问接口的代理对象
			 VideoDao dao = sqlSession.getMapper(VideoDao.class);
			//数据库一共多少条数据
			 count= dao.findCount();
			 //num:跳过多少条数据
			 int num=(pageIndex-1)*size;
			 list=dao.findByVideoClick(num, size);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		pageUtil.setPageIndex(pageIndex);
		pageUtil.setList(list);
		pageUtil.setCount(count);
		pageUtil.setPageNumber((int)Math.ceil((double)count/size));
		return pageUtil;
	}

	@Override
	public int addNoListId(Video video) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			int column = dao.addNoListId(video);
			return column;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return 0;
	}

	@Override
	public int add(Video video) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			int column = dao.add(video);
			return column;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return 0;
	}

	@Override
	public int updateThumbnailUrl(Video video) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			int result = dao.updateThumbnailUrl(video);
			return result;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}

	@Override
	public int updateVideoUrl(Video video) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			int result = dao.updateVideoUrl(video);
			return result;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}

	@Override
	public List<Video> findByListId(int listId) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findByListId(listId);
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return null;
	}

	@Override
	public Video findById(int id) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			Video video = dao.findById(id);
			return video;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return null;
	}
	
	@Override
	public List<Video> findByTopClick() {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findByTopClick();
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return null;
	}
	
	@Override
	public List<Video> findByRecommend() {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findByRecommend();
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return null;
	}
	@Override
	public List<Video> findByStateMulti(int id, int id2, int id3) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findByStateMulti(id, id2, id3);
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}
	@Override
	public List<Video> findByCategoryIdAll(int categoryId) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findByCategoryIdAll(categoryId);
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}
	@Override
	public List<Video> findByRand() {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			List<Video> list = dao.findByRand();
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}
	@Override
	public int updateVideoClick(int videoId) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			int result = dao.updateVideoClick(videoId);
			return result;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}
	@Override
	public int updateVideoClick2(int videoId) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			VideoDao dao = sqlSession.getMapper(VideoDao.class);
			int result = dao.updateVideoClick2(videoId);
			return result;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}

}
